package cn.edu.jssvc.litai.shapedrawabletest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    FragmentManager fragmentManager;
    Fragment shapeFragment;
    TextView titleTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getSupportFragmentManager();
        Fragment fragment= new MainFragment();
        FragmentTransaction  fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.placehold_frame_layout,fragment);
        fragmentTransaction.commit();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.select_shape_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        titleTextView = findViewById(R.id.titleTextView);
        switch (item.getItemId()){
            case R.id.item_rect:
                Log.d("调试", "item_rect1");
                titleTextView.setText("矩形形状可绘制对象示例");
                shapeFragment = new RectFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                break;

            case R.id.item_line:
                Log.d("调试", "item_line1");
                titleTextView.setText("线段形状可绘制对象示例");
                shapeFragment = new LineFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                break;

            case R.id.item_oval:
                Log.d("调试", "item_oval1");
                titleTextView.setText("椭圆与圆形状可绘制对象示例");
                shapeFragment = new OvalFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                break;

            case R.id.item_ring:
                Log.d("调试", "item_ring1");
                titleTextView.setText("圆环形状可绘制对象示例");
                shapeFragment = new RingFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                break;

            case R.id.item_gradient:
                Log.d("调试", "item_gradient1");
                titleTextView.setText("渐变效果示例");
                shapeFragment = new GradientFragment();
                fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.placehold_frame_layout,shapeFragment).commit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}